package com.itheima.util;

import io.jsonwebtoken.*;

import java.util.Date;
import java.util.Map;

// Jwt令牌工具类
public class JwtUtils {

    //密钥
    private static String signKey = "qwsadasdadff";
    private static Long expire = 43200000L;

    /***
     * 生成JWT令牌
     */
    public static String createJwt(Map<String, Object> claims) {
        return Jwts.builder()
                .signWith(SignatureAlgorithm.HS256, signKey)
                .addClaims(claims)
                .setExpiration(new Date(System.currentTimeMillis() + expire))
                .compact();
    }


    /***
     * 解析JWT令牌
     */
    public static Map<String, Object> parseJwt(String jwt) {
        Claims claims = null;
        try {
            claims = Jwts.parser()
                    .setSigningKey(signKey)
                    .parseClaimsJws(jwt)
                    .getBody();
        } catch (Exception e) {
            return null;
        }
        return claims;
    }
}
